package com.sto.stoapp.manager;

import com.sto.stoapp.dataprocess.ProcessUploadData;
import com.sto.stoapp.http.MessageType;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.server.StoDownServer;
import com.sto.stoapp.util.UnifiedStaticType;
import com.sto.stoapp.widget.BaseActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

/**
 * 自定义闹钟管理器
 * 
 * @author Administrator
 * 
 */
public class AlarmClockManager {

	public static final String ALARM_CLOCK_TYPE = "ALARM_CLOCK_TYPE";

	private final static int ONE_DAY_LOGOUT_REQUEST_CODE = 0X10;
	private final static int AUTO_UPLOAD_REQUEST_CODE = 0X11;
	private final static int AUTO_ORDER_REQUEST_CODE = 0X12;
	private Context context;

	private static AlarmClockManager alarmClockManager;

	/**
	 * 构造函数
	 * 
	 * @param context
	 */
	private AlarmClockManager(Context context) {
		this.context = context;

	}

	/**
	 * 得到单例化对象
	 * 
	 * @param context
	 * @return
	 */
	public synchronized static AlarmClockManager getInstance(Context context) {
		if (alarmClockManager == null) {
			alarmClockManager = new AlarmClockManager(context);
		}
		return alarmClockManager;
	}

	/**
	 * 启动24小时登出
	 */
	public void start24Alarm() {

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.add(Calendar.HOUR_OF_DAY, 24);

		PendingIntent sender = PendingIntent.getBroadcast(context,
				ONE_DAY_LOGOUT_REQUEST_CODE,
				buildIntent(BaseActivity.CLOSE_THIS_ACTIVITY_ACTION),
				PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager alarm = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
		System.out.println("AlarmManager login");
	}

	private Intent buildIntent(String action) {
		return new Intent(action);
	}

	/**
	 * 取消一个定时闹钟
	 */
	public void cance24lAlarm() {
		PendingIntent sender = PendingIntent.getBroadcast(context,
				ONE_DAY_LOGOUT_REQUEST_CODE,
				buildIntent(BaseActivity.CLOSE_THIS_ACTIVITY_ACTION),
				PendingIntent.FLAG_UPDATE_CURRENT);
		if (sender == null)
			return;
		AlarmManager alarm = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(sender);
		sender.cancel();
	}

	/**
	 * 启动数据自动上传定时器
	 * 
	 * 5*1000 5秒
	 */
	public void startAutoUploadAlarm(int time) {

		Intent intent = buildIntent(UnifiedStaticType.UP_ACTION);
		intent.putIntegerArrayListExtra(UnifiedStaticType.UPLOAD_TYPE,
				new ProcessUploadData().sendAllData());
		PendingIntent uploadSender = PendingIntent.getBroadcast(context,
				AUTO_UPLOAD_REQUEST_CODE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager uploadTimer = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		uploadTimer.setRepeating(AlarmManager.RTC_WAKEUP,
				System.currentTimeMillis(), time * 60 * 1000, uploadSender);

	}

	public void closeUploadAlarm() {
		Intent intent = buildIntent(UnifiedStaticType.UP_ACTION);
		intent.putIntegerArrayListExtra(UnifiedStaticType.UPLOAD_TYPE,
				new ProcessUploadData().sendAllData());
		PendingIntent uploadSender = PendingIntent.getBroadcast(context,
				AUTO_UPLOAD_REQUEST_CODE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		if (uploadSender == null) {
			ExceptionLog.writeLog("closeUploadTimer", "uploadSender == null");
			return;
		}
		AlarmManager uploadTimer = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		uploadTimer.cancel(uploadSender);
		uploadSender.cancel();
		uploadSender = null;
	}

	/**
	 * 启动订单自动下载定时器
	 */
	public void startAutoOrderAlarm(int time) {

		if (time >= 5) {
			Intent intent = buildIntent(StoDownServer.UPDATE_ACTION);
			intent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
					new int[] { MessageType.ORDER_DOWNLOAD_TYPE });
			PendingIntent orderSender = PendingIntent.getBroadcast(context,
					AUTO_ORDER_REQUEST_CODE, intent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			AlarmManager mgr = (AlarmManager) context
					.getSystemService(Context.ALARM_SERVICE);
			mgr.setRepeating(AlarmManager.RTC_WAKEUP,
					System.currentTimeMillis() + (time * 1000),
					time * 60 * 1000l, orderSender);
		}

	}

	/**
	 * 关闭订单自动下载
	 */
	public void closeAutoOrderAlarm() {
		Intent intent = buildIntent(StoDownServer.UPDATE_ACTION);
		intent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
				new int[] { MessageType.ORDER_DOWNLOAD_TYPE });
		PendingIntent orderSender = PendingIntent.getBroadcast(context,
				AUTO_ORDER_REQUEST_CODE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager mgr = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		mgr.cancel(orderSender);
		orderSender.cancel();
		orderSender = null;
	}
}
